/*
 * Copyright 2009 Hao Nguyen
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package gwt.g3d.client.gl2;

import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.dom.client.CanvasElement;

/**
 * Helper class to extract GL2 context from a canvas.
 * 
 * @author hao1300@gmail.com
 */
public class GL2ContextHelper {
  private static final String[] CONTEXT_IDS =
      { "webgl", "experimental-webgl", "webkit-3d", "moz-webgl" };

  public static GL2 getGL2(Canvas canvas) {
    return getGL2(canvas, null);
  }

  public static GL2 getGL2(Canvas canvas, WebGLContextAttributes attribs) {
    GL2 context = null;
    for (String contextId : CONTEXT_IDS) {
      if (attribs == null) {
        context = (GL2Impl) canvas.getContext(contextId);
      } else {
        context = getContext(canvas.getCanvasElement(), contextId,
            attribs.getGLContextAttributesImpl());
      }
      if (context != null) {
        break;
      }
    }
    return context;
  }
  
  private static native GL2Impl getContext(CanvasElement canvasElem,
      String contextId, JavaScriptObject glContextAttr) /*-{
    return canvasElem.getContext(contextId, glContextAttr);
  }-*/;
}
